package com.zatech.cgnci.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zatech.cgnci.project.base.context.UserContext;
import com.zatech.cgnci.project.base.enums.BusinessScenariosEnum;
import com.zatech.cgnci.project.base.enums.OperationTypeEnum;
import com.zatech.cgnci.project.base.enums.ResponseCodeEnum;
import com.zatech.cgnci.project.base.exception.GlobalException;
import com.zatech.cgnci.project.base.utils.ConvertUtils;
import com.zatech.cgnci.project.base.utils.VerifyUtils;
import com.zatech.cgnci.project.dto.*;
import com.zatech.cgnci.project.entity.*;
import com.zatech.cgnci.project.mapper.CgnciApprovalInfoMapper;
import com.zatech.cgnci.project.mapper.CgnciApprovalRecordMapper;
import com.zatech.cgnci.project.mapper.CgnciEndorsementMapper;
import com.zatech.cgnci.project.service.*;
import com.zatech.cgnci.project.vo.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 批改主表 服务实现类
 * </p>
 *
 * @author Generator
 * @since 2024-12-17
 */
@Service
public class CgnciEndorsementServiceImpl extends ServiceImpl<CgnciEndorsementMapper, CgnciEndorsementDO> implements CgnciEndorsementService {

    @Resource
    private CgnciBusinessInsuranceService cgnciBusinessInsuranceService;
    @Resource
    private CgnciEndorsementDetailService endorsementDetailService;
    @Resource
    private CgnciOperationLogsService cgnciOperationLogsService;
    @Resource
    private CgnciApprovalRecordService approvalRecordService;
    @Resource
    IApprovalService approvalService;
    @Resource
    CgnciApprovalInfoMapper approvalInfoMapper;
    @Resource
    CgnciApprovalRecordMapper approvalRecordMapper;
    @Resource
    CgnciAccessoryService accessoryService;
    @Resource
    private CgnciDirectInsuranceService cgnciDirectInsuranceService;
    @Resource
    private CgnciDirectInsuranceRiskService cgnciDirectInsuranceRiskService;
    @Resource
    private CgnciDirectInsuranceDetailService cgnciDirectInsuranceDetailService;
    @Resource
    private CgnciReinsuranceInfoService cgnciReinsuranceInfoService;
    @Resource
    private CgnciReinsuranceRiskService cgnciReinsuranceRiskService;
    @Resource
    private CgnciReinsuranceRiskInsuredService cgnciReinsuranceRiskInsuredService;
    @Resource
    private CgnciReinsuranceRiskLayerService cgnciReinsuranceRiskLayerService;
    @Resource
    private CgnciUnderwritingPolicyService cgnciUnderwritingPolicyService;
    @Resource
    CgnciReinsurerService reinsurerService;
    @Resource
    CgnciClaimBasisService claimBasisService;
    @Resource
    CgnciAccidentLimitService accidentLimitService;
    @Resource
    CgnciSpecialConditionService specialConditionService;
    @Resource
    CgnciBillInfoService billInfoService;
    @Resource
    CgnciExchangeRateService exchangeRateService;
    @Resource
    CgnciPaymentScheduleService paymentScheduleService;

    @Override
    public IPage<CgnciEndorsementInsurancePageVO> pageEndorsementInsurance(CgnciEndorsementInsuranceQueryDTO queryDTO) {
        return this.baseMapper.pageEndorsementInsurance(queryDTO.toPage(), queryDTO);
    }

    @Override
    public CgnciEndorsementInsuranceVO detail(Long endorsementId, String type) {
        CgnciEndorsementInsuranceVO businessInsuranceVO = new CgnciEndorsementInsuranceVO();
        CgnciBusinessInsuranceDO businessInsuranceDO = cgnciBusinessInsuranceService.getOne(new LambdaQueryWrapper<CgnciBusinessInsuranceDO>()
                .eq(CgnciBusinessInsuranceDO::getLastEndorsementId, endorsementId));
        if (VerifyUtils.isEmpty(businessInsuranceDO)) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "endorsementId有误");
        }
        CgnciEndorsementDO endorsementDO = this.getById(endorsementId);
        // 任务流程和镜像数据1对1
        CgnciApprovalInfoDO approvalInfoDO = approvalInfoMapper.selectOne(new LambdaQueryWrapper<CgnciApprovalInfoDO>()
                .eq(CgnciApprovalInfoDO::getFormId, endorsementDO.getEndorsementId())
                .eq(CgnciApprovalInfoDO::getFormNo, endorsementDO.getFormNo()));
        if (VerifyUtils.isNotEmpty(approvalInfoDO)) {
            businessInsuranceVO.setApprovalInfoId(approvalInfoDO.getApprovalInfoId());
            businessInsuranceVO.setTaskId(approvalInfoDO.getTaskId());
            businessInsuranceVO.setTaskType(approvalInfoDO.getTaskType());
        }

        // 批改信息
        businessInsuranceVO.setEndorsementVO(ConvertUtils.convert(endorsementDO, CgnciEndorsementVO.class));

        // 业务数据
        CgnciBusinessInsuranceMainVO detail = cgnciBusinessInsuranceService.detail(businessInsuranceDO.getInsuranceId(), type);
        businessInsuranceVO.setBusinessInsuranceVO(detail);
        return businessInsuranceVO;
    }

    @Override
    public void updateEndorsementInsurance(CgnciEndorsementInsuranceSaveOrUpdateDTO updateDTO, String type) {
        CgnciEndorsementDO checkEndorsementDO = this.getById(updateDTO.getEndorsementInsuranceDTO().getEndorsementId());
        if (VerifyUtils.isEmpty(checkEndorsementDO)) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "endorsementId有误");
        }
        CgnciEndorsementDO endorsementDO = ConvertUtils.convert(updateDTO.getEndorsementInsuranceDTO(), CgnciEndorsementDO.class);
        this.updateById(endorsementDO);

        // 维护业务名称到工作台
        CgnciApprovalInfoDO approvalInfoDO = approvalInfoMapper.selectOne(new LambdaQueryWrapper<CgnciApprovalInfoDO>()
                .eq(CgnciApprovalInfoDO::getFormId, updateDTO.getEndorsementInsuranceDTO().getEndorsementId()));
        if (VerifyUtils.isEmpty(approvalInfoDO)) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "任务流程不存在");
        }
        if ("1".equals(type)) {
            approvalInfoDO.setBusinessName(updateDTO.getBusinessInsuranceUpdateDTO().getBusinessInsuranceUpdateDTO().getProjectName());
        } else {
            approvalInfoDO.setBusinessName(updateDTO.getBusinessInsuranceUpdateDTO().getBusinessInsuranceUpdateDTO().getBusinessName());
        }
        approvalInfoMapper.updateById(approvalInfoDO);
    }

    @Override
    public void submit(FlowTaskSubmitDTO flowTaskDTO) {
        // 获取当前登录用户
        UserEntity user = UserContext.getUser();
        CgnciEndorsementDO endorsementDO = this.getById(flowTaskDTO.getFormId());
        // 校验是否存在
        if (VerifyUtils.isEmpty(endorsementDO)) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR);
        }
        // 通过批改id查询业务数据
        CgnciBusinessInsuranceDO businessInsuranceDO = cgnciBusinessInsuranceService.getOne(new LambdaQueryWrapper<CgnciBusinessInsuranceDO>()
                .eq(CgnciBusinessInsuranceDO::getLastEndorsementId, endorsementDO.getEndorsementId()));
        if (VerifyUtils.isEmpty(businessInsuranceDO)) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR);
        }
        FlowTaskVO submit = approvalService.submit(flowTaskDTO);
        // 回调提交审核状态流程
        businessInsuranceDO.setStatus(submit.getStatus());
        cgnciBusinessInsuranceService.updateById(businessInsuranceDO);
        //提交审核操作日志
        CgnciOperationLogsDTO operationLogsDTO = new CgnciOperationLogsDTO();
        operationLogsDTO.setBizNo(String.valueOf(endorsementDO.getBusinessId()));
        operationLogsDTO.setBizType(BusinessScenariosEnum.POLICY.getCode());
        operationLogsDTO.setOperationType(getOperationType(businessInsuranceDO.getBusinessType(), "submit"));
        operationLogsDTO.setCreator(user.getNameAndCode());
        cgnciOperationLogsService.saveLog(operationLogsDTO);
        /*ApprovalRecordVO approvalRecordVo = approvalRecordService.getPreviousApprovalRecord(endorsementDO.getBusinessId());
        String content = String.format("{%s}{%s}{%s} 流转至{%s}", BusinessStatusEnum.getDescByCode(submit.getStatus()), approvalRecordVo.getTaskType(),
                approvalRecordVo.getTaskId(), approvalRecordVo.getCurrentApproverAndName());
        operationLogsDTO.setContent(content);
        operationLogsDTO.setOperationType(OperationTypeEnum.TASK_TRANSFER.getCode());
        operationLogsDTO.setCreator("系统");
        cgnciOperationLogsService.saveLog(operationLogsDTO);*/
    }

    @Override
    public void approve(FlowTaskApprovalDTO flowTaskDTO) {
        // 获取当前登录用户
        UserEntity user = UserContext.getUser();
        CgnciEndorsementDO endorsementDO = this.getById(flowTaskDTO.getFormId());
        // 校验是否存在
        if (VerifyUtils.isEmpty(endorsementDO)) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR);
        }
        // 通过批改id查询业务数据
        CgnciBusinessInsuranceDO businessInsuranceDO = cgnciBusinessInsuranceService.getOne(new LambdaQueryWrapper<CgnciBusinessInsuranceDO>()
                .eq(CgnciBusinessInsuranceDO::getLastEndorsementId, endorsementDO.getEndorsementId()));
        if (VerifyUtils.isEmpty(businessInsuranceDO)) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR);
        }
        FlowTaskVO approve = approvalService.approve(flowTaskDTO);
        // 回调审批通过状态流程
        businessInsuranceDO.setStatus(approve.getStatus());

        if (1 == approve.getIsEnd()) {
            businessInsuranceDO.setIsMirror("0");
            businessInsuranceDO.setIsEffective("1");

            // 审批通过之后,根据工作流配置的模块id(模块id可以有多个分组任务,比如新增任务和编辑任务)查询所有的业务数据,
            // 需要把最新审批通过的业务数据变为不是镜像数据,之前不是镜像数据的业务数据变更为镜像数据
            List<CgnciApprovalInfoDO> cgnciApprovalInfoDOS = approvalInfoMapper.selectList(new LambdaQueryWrapper<CgnciApprovalInfoDO>()
                    .eq(CgnciApprovalInfoDO::getFormNo, approve.getFormNo())
                    .eq(CgnciApprovalInfoDO::getStatus, "3")
                    .orderByDesc(CgnciApprovalInfoDO::getCreateTime)
                    .last("limit 2"));
            if (cgnciApprovalInfoDOS.size() == 2) {
                CgnciApprovalInfoDO firstApprovalInfo = cgnciApprovalInfoDOS.get(0);
                CgnciApprovalInfoDO secondApprovalInfo = cgnciApprovalInfoDOS.get(1);
                // 标记最近一条审批通过的业务数据为非镜像数据
                CgnciBusinessInsuranceDO firstBusinessInsurance = cgnciBusinessInsuranceService.getOne(new LambdaQueryWrapper<CgnciBusinessInsuranceDO>()
                        .eq(CgnciBusinessInsuranceDO::getLastEndorsementId, firstApprovalInfo.getFormId()));
                firstBusinessInsurance.setIsMirror("0");
                firstBusinessInsurance.setIsEffective("1");
                cgnciBusinessInsuranceService.updateById(firstBusinessInsurance);

                // 标记之后的一个业务数据为镜像数据
                CgnciBusinessInsuranceDO secondBusinessInsurance;
                secondBusinessInsurance = cgnciBusinessInsuranceService.getOne(new LambdaQueryWrapper<CgnciBusinessInsuranceDO>()
                        .eq(CgnciBusinessInsuranceDO::getLastEndorsementId, secondApprovalInfo.getFormId()));
                if (VerifyUtils.isEmpty(secondBusinessInsurance)) {
                    // 如果最近第二条数据不是批改数据，则要去关联业务数据
                    secondBusinessInsurance = cgnciBusinessInsuranceService.getById(endorsementDO.getBusinessId());
                }
                secondBusinessInsurance.setIsMirror("1");
                secondBusinessInsurance.setIsEffective("0");
                cgnciBusinessInsuranceService.updateById(secondBusinessInsurance);
            }
            //操作日志 记录
            String remark = flowTaskDTO.getApproverOpinions() == null ? "" : flowTaskDTO.getApproverOpinions();
            cgnciOperationLogsService.insertOperationLogs(
                    String.valueOf(businessInsuranceDO.getInsuranceId()),
                    BusinessScenariosEnum.POLICY.getCode(),
                    getOperationType(businessInsuranceDO.getBusinessType(), "pass"),
                    remark,
                    user.getNameAndCode()
            );
        }
        cgnciBusinessInsuranceService.updateById(businessInsuranceDO);

    }

    @Override
    public void back(FlowTaskBackDTO flowTaskDTO) {
        // 获取当前登录用户
        UserEntity user = UserContext.getUser();
        CgnciEndorsementDO endorsementDO = this.getById(flowTaskDTO.getFormId());
        // 校验是否存在
        if (VerifyUtils.isEmpty(endorsementDO)) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR);
        }
        // 通过批改id查询业务数据
        CgnciBusinessInsuranceDO businessInsuranceDO = cgnciBusinessInsuranceService.getOne(new LambdaQueryWrapper<CgnciBusinessInsuranceDO>()
                .eq(CgnciBusinessInsuranceDO::getLastEndorsementId, endorsementDO.getEndorsementId()));
        if (VerifyUtils.isEmpty(businessInsuranceDO)) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR);
        }
        FlowTaskVO back = approvalService.back(flowTaskDTO);
        // 回调审批通过状态流程
        businessInsuranceDO.setStatus(back.getStatus());
        cgnciBusinessInsuranceService.updateById(businessInsuranceDO);
        //退回操作日志记录
        CgnciOperationLogsDTO operationLogsDTO = new CgnciOperationLogsDTO();
        operationLogsDTO.setBizNo(String.valueOf(businessInsuranceDO.getInsuranceId()));
        operationLogsDTO.setBizType(BusinessScenariosEnum.POLICY.getCode());
        operationLogsDTO.setOperationType(getOperationType(businessInsuranceDO.getBusinessType(), "back"));
        operationLogsDTO.setCreator(user.getNameAndCode());
        operationLogsDTO.setContent(flowTaskDTO.getApproverOpinions());
        cgnciOperationLogsService.saveLog(operationLogsDTO);
    }

    @Override
    public void signOut(Long endorsementId) {
        // 获取当前登录用户
        UserEntity user = UserContext.getUser();
        CgnciEndorsementDO endorsementDO = this.getById(endorsementId);
        // 校验是否存在
        if (VerifyUtils.isEmpty(endorsementDO)) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR);
        }
        // 通过批改id查询业务数据
        CgnciBusinessInsuranceDO businessInsuranceDO = cgnciBusinessInsuranceService.getOne(new LambdaQueryWrapper<CgnciBusinessInsuranceDO>()
                .eq(CgnciBusinessInsuranceDO::getLastEndorsementId, endorsementDO.getEndorsementId()));
        if (VerifyUtils.isEmpty(businessInsuranceDO)) {
            throw new GlobalException(ResponseCodeEnum.PARAMS_ILLEGAL);
        }
        // 判断当前状态是否为 录入中
        if (!"0".equals(businessInsuranceDO.getStatus()) && !"2".equals(businessInsuranceDO.getStatus())) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "当前状态不能注销");
        }
        approvalService.cancelTask(FlowTaskCancelDTO.builder().formId(String.valueOf(endorsementDO.getEndorsementId())).formNo(endorsementDO.getFormNo()).build());
        // 回调提交审核状态流程
        businessInsuranceDO.setStatus("4");
        //操作日志
        cgnciOperationLogsService.insertOperationLogs(
                String.valueOf(businessInsuranceDO.getInsuranceId()),
                BusinessScenariosEnum.POLICY.getCode(),
                getOperationType(businessInsuranceDO.getBusinessType(), "signOut"),
                "",
                user.getNameAndCode()
        );
        cgnciBusinessInsuranceService.updateById(businessInsuranceDO);
    }

    @Override
    public List<ApprovalRecordVO> log(Long endorsementId) {
        List<CgnciApprovalRecordDO> approvalRecordDOList = approvalRecordMapper.selectList(new LambdaQueryWrapper<CgnciApprovalRecordDO>().eq(CgnciApprovalRecordDO::getFormId, endorsementId));
        return ConvertUtils.convert(approvalRecordDOList, ApprovalRecordVO.class);
    }

    @Override
    public void nextInsuranceCreate(CgnciEndorsementInsuranceNextCreateDTO addCreateDTO, Long endorsementId) {
        // 获取当前登录用户
        UserEntity user = UserContext.getUser();
        // 获取原信息
        CgnciBusinessInsuranceMainVO insuranceVO = cgnciBusinessInsuranceService.detail(addCreateDTO.getInsuranceId(), "1");

        // 基本信息
        CgnciBusinessInsuranceDO businessInsuranceDO = ConvertUtils.convert(insuranceVO.getBusinessInsuranceVO(), CgnciBusinessInsuranceDO.class);
        businessInsuranceDO.setInsuranceId(null);
        // 设置该数据为镜像数据
        businessInsuranceDO.setIsMirror("1");
        businessInsuranceDO.setStatus("0");
        businessInsuranceDO.setIsEffective("0");
        businessInsuranceDO.setLastEndorsementId(endorsementId);
        businessInsuranceDO.setCreateUser(user.getNameAndCode());
        businessInsuranceDO.setCreateTime(new Date());
        businessInsuranceDO.setUpdateUser(user.getNameAndCode());
        businessInsuranceDO.setUpdateTime(new Date());
        cgnciBusinessInsuranceService.save(businessInsuranceDO);

        // 直保信息
        CgnciDirectInsuranceVO directInsuranceVO = insuranceVO.getDirectInsuranceVO();
        CgnciDirectInsuranceDO directInsuranceDO = ConvertUtils.convert(directInsuranceVO, CgnciDirectInsuranceDO.class);
        directInsuranceDO.setDirectId(null);
        directInsuranceDO.setBusinessId(businessInsuranceDO.getInsuranceId());
        directInsuranceDO.setCreateUser(user.getNameAndCode());
        directInsuranceDO.setUpdateUser(user.getNameAndCode());
        cgnciDirectInsuranceService.save(directInsuranceDO);

        // 直保信息--险类信息
        List<CgnciDirectInsuranceRiskVO> directRisksVO = directInsuranceVO.getInsuranceRisks();
        if (VerifyUtils.isNotEmpty(directRisksVO)) {
            directRisksVO.forEach(risk -> {
                CgnciDirectInsuranceRiskDO insuranceRiskDO = ConvertUtils.convert(risk, CgnciDirectInsuranceRiskDO.class);
                insuranceRiskDO.setRiskId(null);
                insuranceRiskDO.setDirectInsuranceId(directInsuranceDO.getDirectId());
                insuranceRiskDO.setCreateUser(user.getNameAndCode());
                insuranceRiskDO.setUpdateUser(user.getNameAndCode());
                cgnciDirectInsuranceRiskService.save(insuranceRiskDO);

                List<CgnciDirectInsuranceDetailVO> detailsVOList = risk.getDetails();
                if (VerifyUtils.isNotEmpty(detailsVOList)) {
                    List<CgnciDirectInsuranceDetailDO> detailsDOList = ConvertUtils.convert(detailsVOList, CgnciDirectInsuranceDetailDO.class);
                    detailsDOList.forEach(detail -> {
                        detail.setDetailId(null);
                        detail.setDirectId(insuranceRiskDO.getRiskId());
                        detail.setCreateUser(user.getNameAndCode());
                        detail.setUpdateUser(user.getNameAndCode());
                    });
                    cgnciDirectInsuranceDetailService.saveBatch(detailsDOList);
                }
            });
        }


        // 分入信息
        CgnciReinsuranceInfoVO divideInVO = insuranceVO.getDivideInVO();
        if (VerifyUtils.isNotEmpty(divideInVO)) {
            CgnciReinsuranceInfoDO divideInDO = ConvertUtils.convert(divideInVO, CgnciReinsuranceInfoDO.class);
            divideInDO.setReinsuranceId(null);
            divideInDO.setBusinessId(businessInsuranceDO.getInsuranceId());
            divideInDO.setCreateUser(user.getNameAndCode());
            divideInDO.setUpdateUser(user.getNameAndCode());
            cgnciReinsuranceInfoService.save(divideInDO);
            // 分入信息--险类信息
            List<CgnciReinsuranceRiskVO> divideInRiskVOList = divideInVO.getRisks();
            if (VerifyUtils.isNotEmpty(divideInRiskVOList)) {
                divideInRiskVOList.forEach(risk -> {
                    CgnciReinsuranceRiskDO riskDO = ConvertUtils.convert(risk, CgnciReinsuranceRiskDO.class);
                    riskDO.setReinsuranceRiskId(null);
                    riskDO.setReinsuranceId(divideInDO.getReinsuranceId());
                    riskDO.setCreateUser(user.getNameAndCode());
                    riskDO.setUpdateUser(user.getNameAndCode());
                    cgnciReinsuranceRiskService.save(riskDO);

                    // 分人信息--险类信息-标的
                    List<CgnciReinsuranceRiskInsuredVO> insuredsVO = risk.getRiskInsureds();
                    if (VerifyUtils.isNotEmpty(insuredsVO)) {
                        insuredsVO.forEach(riskInsured -> {
                            CgnciReinsuranceRiskInsuredDO insuredDO = ConvertUtils.convert(riskInsured, CgnciReinsuranceRiskInsuredDO.class);
                            insuredDO.setRiskInsuredId(null);
                            insuredDO.setReinsuranceRiskId(riskDO.getReinsuranceRiskId());
                            insuredDO.setCreateUser(user.getNameAndCode());
                            insuredDO.setUpdateUser(user.getNameAndCode());
                            cgnciReinsuranceRiskInsuredService.save(insuredDO);

                            List<CgnciReinsuranceRiskLayerVO> riskLayersVO = riskInsured.getRiskLayers();
                            List<CgnciReinsuranceRiskLayerDO> riskLayersDO = ConvertUtils.convert(riskLayersVO, CgnciReinsuranceRiskLayerDO.class);
                            riskLayersDO.forEach(riskLayer -> {
                                riskLayer.setLayerId(null);
                                riskLayer.setReinsuranceRiskId(insuredDO.getRiskInsuredId());
                                riskLayer.setCreateUser(user.getNameAndCode());
                                riskLayer.setUpdateUser(user.getNameAndCode());
                            });
                            cgnciReinsuranceRiskLayerService.saveBatch(riskLayersDO);
                        });
                    }

                });

            }
            // 分入信息--分入方案
            List<CgnciReinsurerVO> divideInReinsurers = divideInVO.getReinsurers();
            if (VerifyUtils.isNotEmpty(divideInReinsurers)) {
                divideInReinsurers.forEach(reinsurer -> {
                    CgnciReinsurerDO reinsurerDO = ConvertUtils.convert(reinsurer, CgnciReinsurerDO.class);
                    reinsurerDO.setReinsurerId(null);
                    reinsurerDO.setReinsuranceId(divideInDO.getReinsuranceId());
                    reinsurerDO.setCreateUser(user.getNameAndCode());
                    reinsurerDO.setUpdateUser(user.getNameAndCode());
                    reinsurerService.save(reinsurerDO);

                    // 保存文件关联
                    List<CgnciAccessoryVO> accessoryVOS = reinsurer.getAccessoryVO();
                    if (VerifyUtils.isNotEmpty(accessoryVOS)) {
                        List<CgnciAccessoryDO> accessoryDOS = ConvertUtils.convert(accessoryVOS, CgnciAccessoryDO.class);
                        accessoryDOS.forEach(accessoryDO -> {
                            accessoryDO.setAccId(null);
                            accessoryDO.setBusinessId(reinsurerDO.getReinsurerId());
                            accessoryDO.setCreateUser(user.getNameAndCode());
                            accessoryDO.setUpdateUser(user.getNameAndCode());
                        });
                        accessoryService.saveBatch(accessoryDOS);
                    }

                    List<CgnciReinsurerRiskVO> risks = reinsurer.getRisks();
                    if (VerifyUtils.isNotEmpty(risks)) {
                        risks.forEach(risk -> {
                            CgnciReinsuranceRiskDO riskDO = ConvertUtils.convert(risk, CgnciReinsuranceRiskDO.class);
                            riskDO.setReinsuranceRiskId(null);
                            riskDO.setReinsuranceId(reinsurerDO.getReinsurerId());
                            riskDO.setCreateUser(user.getNameAndCode());
                            riskDO.setUpdateUser(user.getNameAndCode());
                            cgnciReinsuranceRiskService.save(riskDO);

                            // 分入信息--分入方案--险类信息--标的
                            List<CgnciReinsurerRiskInsuredVO> riskInsureds = risk.getRiskInsureds();
                            if (VerifyUtils.isNotEmpty(riskInsureds)) {
                                riskInsureds.forEach(riskInsured -> {
                                    CgnciReinsuranceRiskInsuredDO insuredDO = ConvertUtils.convert(riskInsured, CgnciReinsuranceRiskInsuredDO.class);
                                    insuredDO.setRiskInsuredId(null);
                                    insuredDO.setReinsuranceRiskId(riskDO.getReinsuranceRiskId());
                                    insuredDO.setCreateUser(user.getNameAndCode());
                                    insuredDO.setUpdateUser(user.getNameAndCode());
                                    cgnciReinsuranceRiskInsuredService.save(insuredDO);

                                    List<CgnciReinsurerRiskLayerVO> riskLayersVO = riskInsured.getRiskLayers();
                                    if (VerifyUtils.isNotEmpty(riskLayersVO)) {
                                        List<CgnciReinsuranceRiskLayerDO> riskLayersDO = ConvertUtils.convert(riskLayersVO, CgnciReinsuranceRiskLayerDO.class);
                                        riskLayersDO.forEach(riskLayer -> {
                                            riskLayer.setLayerId(null);
                                            riskLayer.setReinsuranceRiskId(insuredDO.getRiskInsuredId());
                                            riskLayer.setCreateUser(user.getNameAndCode());
                                            riskLayer.setUpdateUser(user.getNameAndCode());
                                        });
                                        cgnciReinsuranceRiskLayerService.saveBatch(riskLayersDO);

                                    }
                                });
                            }

                        });

                    }
                });

            }

        }

        // 超净自留处理方式
        List<CgnciUnderwritingPolicyVO> underwritingPolicyVOList = insuranceVO.getUnderwritingPolicyVO();
        List<CgnciUnderwritingPolicyDO> underwritingPolicyDOList = ConvertUtils.convert(underwritingPolicyVOList, CgnciUnderwritingPolicyDO.class);
        underwritingPolicyDOList.forEach(policy -> {
            policy.setPolicyId(null);
            policy.setBusinessId(businessInsuranceDO.getInsuranceId());
            policy.setCreateUser(user.getNameAndCode());
            policy.setUpdateUser(user.getNameAndCode());
        });
        cgnciUnderwritingPolicyService.saveBatch(underwritingPolicyDOList);


        // 临分分出信息
        CgnciReinsuranceInfoVO lfDivideOutVO = insuranceVO.getLfDivideOutVO();
        if (VerifyUtils.isNotEmpty(lfDivideOutVO)) {
            CgnciReinsuranceInfoDO lfDivideOuDO = ConvertUtils.convert(lfDivideOutVO, CgnciReinsuranceInfoDO.class);
            lfDivideOuDO.setReinsuranceId(null);
            lfDivideOuDO.setBusinessId(businessInsuranceDO.getInsuranceId());
            lfDivideOuDO.setCreateUser(user.getNameAndCode());
            lfDivideOuDO.setUpdateUser(user.getNameAndCode());
            cgnciReinsuranceInfoService.save(lfDivideOuDO);
            // 临分分出信息--险类信息
            List<CgnciReinsuranceRiskVO> lfDivideOutRiskVOList = lfDivideOutVO.getRisks();
            if (VerifyUtils.isNotEmpty(lfDivideOutRiskVOList)) {
                lfDivideOutRiskVOList.forEach(risk -> {
                    CgnciReinsuranceRiskDO riskDO = ConvertUtils.convert(risk, CgnciReinsuranceRiskDO.class);
                    riskDO.setReinsuranceRiskId(null);
                    riskDO.setReinsuranceId(lfDivideOuDO.getReinsuranceId());
                    riskDO.setCreateUser(user.getNameAndCode());
                    riskDO.setUpdateUser(user.getNameAndCode());
                    cgnciReinsuranceRiskService.save(riskDO);

                    // 临分分出信息--险类信息--标的
                    List<CgnciReinsuranceRiskInsuredVO> riskInsureds = risk.getRiskInsureds();
                    if (VerifyUtils.isNotEmpty(riskInsureds)) {
                        riskInsureds.forEach(riskInsured -> {
                            CgnciReinsuranceRiskInsuredDO insuredDO = ConvertUtils.convert(riskInsured, CgnciReinsuranceRiskInsuredDO.class);
                            insuredDO.setRiskInsuredId(null);
                            insuredDO.setReinsuranceRiskId(riskDO.getReinsuranceRiskId());
                            insuredDO.setCreateUser(user.getNameAndCode());
                            insuredDO.setUpdateUser(user.getNameAndCode());
                            cgnciReinsuranceRiskInsuredService.save(insuredDO);

                            List<CgnciReinsuranceRiskLayerVO> riskLayersVO = riskInsured.getRiskLayers();
                            if (VerifyUtils.isNotEmpty(riskLayersVO)) {
                                List<CgnciReinsuranceRiskLayerDO> riskLayersDO = ConvertUtils.convert(riskLayersVO, CgnciReinsuranceRiskLayerDO.class);
                                riskLayersDO.forEach(riskLayer -> {
                                    riskLayer.setLayerId(null);
                                    riskLayer.setReinsuranceRiskId(insuredDO.getRiskInsuredId());
                                    riskLayer.setCreateUser(user.getNameAndCode());
                                    riskLayer.setUpdateUser(user.getNameAndCode());
                                });
                                cgnciReinsuranceRiskLayerService.saveBatch(riskLayersDO);

                            }
                        });
                    }

                });

            }
            // 临分分出信息--再保人
            List<CgnciReinsurerVO> lfDivideOutReinsurers = lfDivideOutVO.getReinsurers();
            if (VerifyUtils.isNotEmpty(lfDivideOutReinsurers)) {
                lfDivideOutReinsurers.forEach(reinsurer -> {
                    CgnciReinsurerDO reinsurerDO = ConvertUtils.convert(reinsurer, CgnciReinsurerDO.class);
                    reinsurerDO.setReinsurerId(null);
                    reinsurerDO.setReinsuranceId(lfDivideOuDO.getReinsuranceId());
                    reinsurerDO.setCreateUser(user.getNameAndCode());
                    reinsurerDO.setUpdateUser(user.getNameAndCode());
                    reinsurerService.save(reinsurerDO);
                    // 临分分出信息--再保人--险类信息
                    List<CgnciReinsurerRiskVO> risks = reinsurer.getRisks();
                    if (VerifyUtils.isNotEmpty(risks)) {
                        risks.forEach(risk -> {
                            CgnciReinsuranceRiskDO riskDO = ConvertUtils.convert(risk, CgnciReinsuranceRiskDO.class);
                            riskDO.setReinsuranceRiskId(null);
                            riskDO.setReinsuranceId(reinsurerDO.getReinsurerId());
                            riskDO.setCreateUser(user.getNameAndCode());
                            riskDO.setUpdateUser(user.getNameAndCode());
                            cgnciReinsuranceRiskService.save(riskDO);

                            // 临分分出信息--再保人--险类信息--标的
                            List<CgnciReinsurerRiskInsuredVO> riskInsureds = risk.getRiskInsureds();
                            if (VerifyUtils.isNotEmpty(riskInsureds)) {
                                riskInsureds.forEach(riskInsured -> {
                                    CgnciReinsuranceRiskInsuredDO insuredDO = ConvertUtils.convert(riskInsured, CgnciReinsuranceRiskInsuredDO.class);
                                    insuredDO.setRiskInsuredId(null);
                                    insuredDO.setReinsuranceRiskId(riskDO.getReinsuranceRiskId());
                                    insuredDO.setCreateUser(user.getNameAndCode());
                                    insuredDO.setUpdateUser(user.getNameAndCode());
                                    cgnciReinsuranceRiskInsuredService.save(insuredDO);

                                    List<CgnciReinsurerRiskLayerVO> riskLayersVO = riskInsured.getRiskLayers();
                                    List<CgnciReinsuranceRiskLayerDO> riskLayersDO = ConvertUtils.convert(riskLayersVO, CgnciReinsuranceRiskLayerDO.class);
                                    riskLayersDO.forEach(riskLayer -> {
                                        riskLayer.setLayerId(null);
                                        riskLayer.setReinsuranceRiskId(insuredDO.getRiskInsuredId());
                                        riskLayer.setCreateUser(user.getNameAndCode());
                                        riskLayer.setUpdateUser(user.getNameAndCode());
                                    });
                                    cgnciReinsuranceRiskLayerService.saveBatch(riskLayersDO);


                                });
                            }

                        });

                    }

                    // 缴费期次
                    List<CgnciPaymentScheduleVO> payments = reinsurer.getPayments();
                    if (VerifyUtils.isNotEmpty(payments)) {
                        List<CgnciPaymentScheduleDO> paymentList = ConvertUtils.convert(payments, CgnciPaymentScheduleDO.class);
                        paymentList.forEach(payment -> {
                            payment.setScheduleId(null);
                            payment.setReinsuranceId(reinsurerDO.getReinsurerId());
                            payment.setCreateUser(user.getNameAndCode());
                            payment.setUpdateUser(user.getNameAndCode());
                        });
                        paymentScheduleService.saveBatch(paymentList);

                    }

                    // 保费变化量
                    CgnciEndorsementDetailDO endorsementDetailDO = new CgnciEndorsementDetailDO();
                    endorsementDetailDO.setReinsurerId(reinsurerDO.getReinsurerId());
                    endorsementDetailDO.setGrossPremium(reinsurer.getEngagedGrossPremium());
                    endorsementDetailDO.setCommission(reinsurer.getEngagedCommission());
                    endorsementDetailDO.setNetPremium(reinsurer.getEngagedNetPremium());
                    endorsementDetailDO.setCreateUser(user.getNameAndCode());
                    endorsementDetailDO.setUpdateUser(user.getNameAndCode());
                    endorsementDetailService.save(endorsementDetailDO);
                });

            }

        }

        // 合约分出
        List<CgnciReinsuranceInfoVO> hyDivideOutVOList = insuranceVO.getHyDivideOutVOList();
        if (VerifyUtils.isNotEmpty(hyDivideOutVOList)) {
            hyDivideOutVOList.forEach(hyDivideOutVO -> {
                CgnciReinsuranceInfoDO hyDivideOutDO = ConvertUtils.convert(hyDivideOutVO, CgnciReinsuranceInfoDO.class);
                hyDivideOutDO.setReinsuranceId(null);
                hyDivideOutDO.setBusinessId(businessInsuranceDO.getInsuranceId());
                hyDivideOutDO.setCreateUser(user.getNameAndCode());
                hyDivideOutDO.setUpdateUser(user.getNameAndCode());
                cgnciReinsuranceInfoService.save(hyDivideOutDO);

                List<CgnciReinsuranceRiskVO> risks = hyDivideOutVO.getRisks();
                if (VerifyUtils.isNotEmpty(risks)) {
                    risks.forEach(risk -> {
                        CgnciReinsuranceRiskDO riskDO = ConvertUtils.convert(risk, CgnciReinsuranceRiskDO.class);
                        riskDO.setReinsuranceRiskId(null);
                        riskDO.setReinsuranceId(hyDivideOutDO.getReinsuranceId());
                        riskDO.setCreateUser(user.getNameAndCode());
                        riskDO.setUpdateUser(user.getNameAndCode());
                        cgnciReinsuranceRiskService.save(riskDO);

                        // 合约分出信息--再保人--险类信息--标的
                        List<CgnciReinsuranceRiskInsuredVO> riskInsureds = risk.getRiskInsureds();
                        if (VerifyUtils.isNotEmpty(riskInsureds)) {
                            riskInsureds.forEach(riskInsured -> {
                                CgnciReinsuranceRiskInsuredDO insuredDO = ConvertUtils.convert(riskInsured, CgnciReinsuranceRiskInsuredDO.class);
                                insuredDO.setRiskInsuredId(null);
                                insuredDO.setReinsuranceRiskId(riskDO.getReinsuranceRiskId());
                                insuredDO.setCreateUser(user.getNameAndCode());
                                insuredDO.setUpdateUser(user.getNameAndCode());
                                cgnciReinsuranceRiskInsuredService.save(insuredDO);

                                List<CgnciReinsuranceRiskLayerVO> layers = riskInsured.getRiskLayers();
                                if (VerifyUtils.isNotEmpty(layers)) {
                                    List<CgnciReinsuranceRiskLayerDO> riskLayersDO = ConvertUtils.convert(layers, CgnciReinsuranceRiskLayerDO.class);
                                    riskLayersDO.forEach(riskLayer -> {
                                        riskLayer.setLayerId(null);
                                        riskLayer.setReinsuranceRiskId(insuredDO.getRiskInsuredId());
                                        riskLayer.setCreateUser(user.getNameAndCode());
                                        riskLayer.setUpdateUser(user.getNameAndCode());
                                    });
                                    cgnciReinsuranceRiskLayerService.saveBatch(riskLayersDO);
                                }

                            });
                        }

                    });
                }
            });
        }

        // 文件信息
        List<CgnciAccessoryVO> accessoryVOList = insuranceVO.getAccessoryVOList();
        if (VerifyUtils.isNotEmpty(accessoryVOList)) {
            List<CgnciAccessoryDO> accessoryList = new ArrayList<>();
            accessoryVOList.forEach(accessory -> {
                CgnciAccessoryDO accessoryDO = accessoryService.getById(accessory.getAccId());
                // 设置插入数据
                accessoryDO.setAccId(null);
                accessoryDO.setBusinessId(businessInsuranceDO.getInsuranceId());
                accessoryDO.setCreateUser(user.getNameAndCode());
                accessoryDO.setUpdateUser(user.getNameAndCode());
                accessoryList.add(accessoryDO);
            });
            accessoryService.saveBatch(accessoryList);
        }
    }

    @Override
    public void nextDivideInCreate(CgnciEndorsementInsuranceNextCreateDTO addCreateDTO, Long endorsementId) {
        // 获取当前登录用户
        UserEntity user = UserContext.getUser();
        // 获取原信息
        CgnciBusinessInsuranceMainVO insuranceVO = cgnciBusinessInsuranceService.detail(addCreateDTO.getInsuranceId(), "2");

        // 基本信息
        CgnciBusinessInsuranceDO businessInsuranceDO = ConvertUtils.convert(insuranceVO.getBusinessInsuranceVO(), CgnciBusinessInsuranceDO.class);
        businessInsuranceDO.setInsuranceId(null);
        // 设置该数据为镜像数据
        businessInsuranceDO.setIsMirror("1");
        businessInsuranceDO.setStatus("0");
        businessInsuranceDO.setIsEffective("0");
        businessInsuranceDO.setLastEndorsementId(endorsementId);
        businessInsuranceDO.setCreateUser(user.getNameAndCode());
        businessInsuranceDO.setCreateTime(new Date());
        businessInsuranceDO.setUpdateUser(user.getNameAndCode());
        businessInsuranceDO.setUpdateTime(new Date());
        cgnciBusinessInsuranceService.save(businessInsuranceDO);

        // 直保信息
        CgnciDirectInsuranceVO directInsuranceVO = insuranceVO.getDirectInsuranceVO();
        if (VerifyUtils.isNotEmpty(directInsuranceVO)) {
            CgnciDirectInsuranceDO directInsuranceDO = ConvertUtils.convert(directInsuranceVO, CgnciDirectInsuranceDO.class);
            directInsuranceDO.setDirectId(null);
            directInsuranceDO.setBusinessId(businessInsuranceDO.getInsuranceId());
            directInsuranceDO.setCreateUser(user.getNameAndCode());
            directInsuranceDO.setUpdateUser(user.getNameAndCode());
            cgnciDirectInsuranceService.save(directInsuranceDO);

            // 直保信息--险类信息
            List<CgnciDirectInsuranceRiskVO> directRisksVO = directInsuranceVO.getInsuranceRisks();
            if (VerifyUtils.isNotEmpty(directRisksVO)) {
                directRisksVO.forEach(risk -> {
                    CgnciDirectInsuranceRiskDO insuranceRiskDO = ConvertUtils.convert(risk, CgnciDirectInsuranceRiskDO.class);
                    insuranceRiskDO.setRiskId(null);
                    insuranceRiskDO.setDirectInsuranceId(directInsuranceDO.getDirectId());
                    insuranceRiskDO.setCreateUser(user.getNameAndCode());
                    insuranceRiskDO.setUpdateUser(user.getNameAndCode());
                    cgnciDirectInsuranceRiskService.save(insuranceRiskDO);

                    List<CgnciDirectInsuranceDetailVO> detailsVOList = risk.getDetails();
                    if (VerifyUtils.isNotEmpty(detailsVOList)) {
                        List<CgnciDirectInsuranceDetailDO> detailsDOList = ConvertUtils.convert(detailsVOList, CgnciDirectInsuranceDetailDO.class);
                        detailsDOList.forEach(detail -> {
                            detail.setDetailId(null);
                            detail.setDirectId(insuranceRiskDO.getRiskId());
                            detail.setCreateUser(user.getNameAndCode());
                            detail.setUpdateUser(user.getNameAndCode());
                        });
                        cgnciDirectInsuranceDetailService.saveBatch(detailsDOList);

                    }
                });
            }

        }


        // 分入信息
        CgnciReinsuranceInfoVO divideInVO = insuranceVO.getDivideInVO();
        if (VerifyUtils.isNotEmpty(divideInVO)) {
            CgnciReinsuranceInfoDO divideInDO = ConvertUtils.convert(divideInVO, CgnciReinsuranceInfoDO.class);
            divideInDO.setReinsuranceId(null);
            divideInDO.setBusinessId(businessInsuranceDO.getInsuranceId());
            divideInDO.setCreateUser(user.getNameAndCode());
            divideInDO.setUpdateUser(user.getNameAndCode());
            cgnciReinsuranceInfoService.save(divideInDO);
            // 分入信息--险类信息
            List<CgnciReinsuranceRiskVO> divideInRiskVOList = divideInVO.getRisks();
            if (VerifyUtils.isNotEmpty(divideInRiskVOList)) {
                divideInRiskVOList.forEach(risk -> {
                    CgnciReinsuranceRiskDO riskDO = ConvertUtils.convert(risk, CgnciReinsuranceRiskDO.class);
                    riskDO.setReinsuranceRiskId(null);
                    riskDO.setReinsuranceId(divideInDO.getReinsuranceId());
                    riskDO.setCreateUser(user.getNameAndCode());
                    riskDO.setUpdateUser(user.getNameAndCode());
                    cgnciReinsuranceRiskService.save(riskDO);

                    // 分入信息--分入方案--险类信息--标的
                    List<CgnciReinsuranceRiskInsuredVO> insuredsVO = risk.getRiskInsureds();
                    if (VerifyUtils.isNotEmpty(insuredsVO)) {
                        insuredsVO.forEach(riskInsured -> {
                            CgnciReinsuranceRiskInsuredDO insuredDO = ConvertUtils.convert(riskInsured, CgnciReinsuranceRiskInsuredDO.class);
                            insuredDO.setRiskInsuredId(null);
                            insuredDO.setReinsuranceRiskId(riskDO.getReinsuranceRiskId());
                            insuredDO.setCreateUser(user.getNameAndCode());
                            insuredDO.setUpdateUser(user.getNameAndCode());
                            cgnciReinsuranceRiskInsuredService.save(insuredDO);

                            List<CgnciReinsuranceRiskLayerVO> riskLayersVO = riskInsured.getRiskLayers();
                            if (VerifyUtils.isNotEmpty(riskLayersVO)) {
                                List<CgnciReinsuranceRiskLayerDO> riskLayersDO = ConvertUtils.convert(riskLayersVO, CgnciReinsuranceRiskLayerDO.class);
                                riskLayersDO.forEach(riskLayer -> {
                                    riskLayer.setLayerId(null);
                                    riskLayer.setReinsuranceRiskId(insuredDO.getRiskInsuredId());
                                    riskLayer.setCreateUser(user.getNameAndCode());
                                    riskLayer.setUpdateUser(user.getNameAndCode());
                                });
                                cgnciReinsuranceRiskLayerService.saveBatch(riskLayersDO);

                            }
                        });
                    }

                });

            }
            // 分入信息--分入方案
            List<CgnciReinsurerVO> divideInReinsurers = divideInVO.getReinsurers();
            if (VerifyUtils.isNotEmpty(divideInReinsurers)) {
                divideInReinsurers.forEach(reinsurer -> {
                    CgnciReinsurerDO reinsurerDO = ConvertUtils.convert(reinsurer, CgnciReinsurerDO.class);
                    reinsurerDO.setReinsurerId(null);
                    reinsurerDO.setReinsuranceId(divideInDO.getReinsuranceId());
                    reinsurerDO.setCreateUser(user.getNameAndCode());
                    reinsurerDO.setUpdateUser(user.getNameAndCode());
                    reinsurerService.save(reinsurerDO);

                    // 保存文件关联
                    List<CgnciAccessoryVO> accessoryVOS = reinsurer.getAccessoryVO();
                    if (VerifyUtils.isNotEmpty(accessoryVOS)) {
                        List<CgnciAccessoryDO> accessoryDOS = ConvertUtils.convert(accessoryVOS, CgnciAccessoryDO.class);
                        accessoryDOS.forEach(accessoryDO -> {
                            accessoryDO.setAccId(null);
                            accessoryDO.setBusinessId(reinsurerDO.getReinsurerId());
                            accessoryDO.setCreateUser(user.getNameAndCode());
                            accessoryDO.setUpdateUser(user.getNameAndCode());
                        });
                        accessoryService.saveBatch(accessoryDOS);
                    }

                    List<CgnciReinsurerRiskVO> risks = reinsurer.getRisks();
                    if (VerifyUtils.isNotEmpty(risks)) {
                        // 分入信息--分入方案--险类信息
                        risks.forEach(risk -> {
                            CgnciReinsuranceRiskDO riskDO = ConvertUtils.convert(risk, CgnciReinsuranceRiskDO.class);
                            riskDO.setReinsuranceRiskId(null);
                            riskDO.setReinsuranceId(reinsurerDO.getReinsurerId());
                            riskDO.setCreateUser(user.getNameAndCode());
                            riskDO.setUpdateUser(user.getNameAndCode());
                            cgnciReinsuranceRiskService.save(riskDO);

                            // 分入信息--分入方案--险类信息--标的
                            List<CgnciReinsurerRiskInsuredVO> riskInsureds = risk.getRiskInsureds();
                            if (VerifyUtils.isNotEmpty(riskInsureds)) {
                                riskInsureds.forEach(riskInsured -> {
                                    CgnciReinsuranceRiskInsuredDO insuredDO = ConvertUtils.convert(riskInsured, CgnciReinsuranceRiskInsuredDO.class);
                                    insuredDO.setRiskInsuredId(null);
                                    insuredDO.setReinsuranceRiskId(riskDO.getReinsuranceRiskId());
                                    insuredDO.setCreateUser(user.getNameAndCode());
                                    insuredDO.setUpdateUser(user.getNameAndCode());
                                    cgnciReinsuranceRiskInsuredService.save(insuredDO);

                                    List<CgnciReinsurerRiskLayerVO> riskLayersVO = riskInsured.getRiskLayers();
                                    if (VerifyUtils.isNotEmpty(riskLayersVO)) {
                                        List<CgnciReinsuranceRiskLayerDO> riskLayersDO = ConvertUtils.convert(riskLayersVO, CgnciReinsuranceRiskLayerDO.class);
                                        riskLayersDO.forEach(riskLayer -> {
                                            riskLayer.setLayerId(null);
                                            riskLayer.setReinsuranceRiskId(insuredDO.getRiskInsuredId());
                                            riskLayer.setCreateUser(user.getNameAndCode());
                                            riskLayer.setUpdateUser(user.getNameAndCode());
                                        });
                                        cgnciReinsuranceRiskLayerService.saveBatch(riskLayersDO);

                                    }
                                });
                            }

                        });

                    }
                });

            }

        }

        // 超净自留处理方式
        List<CgnciUnderwritingPolicyVO> underwritingPolicyVOList = insuranceVO.getUnderwritingPolicyVO();
        List<CgnciUnderwritingPolicyDO> underwritingPolicyDOList = ConvertUtils.convert(underwritingPolicyVOList, CgnciUnderwritingPolicyDO.class);
        underwritingPolicyDOList.forEach(policy -> {
            policy.setPolicyId(null);
            policy.setBusinessId(businessInsuranceDO.getInsuranceId());
            policy.setCreateUser(user.getNameAndCode());
            policy.setUpdateUser(user.getNameAndCode());
        });
        cgnciUnderwritingPolicyService.saveBatch(underwritingPolicyDOList);


        // 临分分出信息
        CgnciReinsuranceInfoVO lfDivideOutVO = insuranceVO.getLfDivideOutVO();
        if (VerifyUtils.isNotEmpty(lfDivideOutVO)) {
            CgnciReinsuranceInfoDO lfDivideOuDO = ConvertUtils.convert(lfDivideOutVO, CgnciReinsuranceInfoDO.class);
            lfDivideOuDO.setReinsuranceId(null);
            lfDivideOuDO.setBusinessId(businessInsuranceDO.getInsuranceId());
            lfDivideOuDO.setCreateUser(user.getNameAndCode());
            lfDivideOuDO.setUpdateUser(user.getNameAndCode());
            cgnciReinsuranceInfoService.save(lfDivideOuDO);
            // 临分分出信息--险类信息
            List<CgnciReinsuranceRiskVO> lfDivideOutRiskVOList = lfDivideOutVO.getRisks();
            if (VerifyUtils.isNotEmpty(lfDivideOutRiskVOList)) {
                lfDivideOutRiskVOList.forEach(risk -> {
                    CgnciReinsuranceRiskDO riskDO = ConvertUtils.convert(risk, CgnciReinsuranceRiskDO.class);
                    riskDO.setReinsuranceRiskId(null);
                    riskDO.setReinsuranceId(lfDivideOuDO.getReinsuranceId());
                    riskDO.setCreateUser(user.getNameAndCode());
                    riskDO.setUpdateUser(user.getNameAndCode());
                    cgnciReinsuranceRiskService.save(riskDO);

                    // 临分分出信息--险类信息--标的
                    List<CgnciReinsuranceRiskInsuredVO> insuredsVO = risk.getRiskInsureds();
                    if (VerifyUtils.isNotEmpty(insuredsVO)) {
                        insuredsVO.forEach(riskInsured -> {
                            CgnciReinsuranceRiskInsuredDO insuredDO = ConvertUtils.convert(riskInsured, CgnciReinsuranceRiskInsuredDO.class);
                            insuredDO.setRiskInsuredId(null);
                            insuredDO.setReinsuranceRiskId(riskDO.getReinsuranceRiskId());
                            insuredDO.setCreateUser(user.getNameAndCode());
                            insuredDO.setUpdateUser(user.getNameAndCode());
                            cgnciReinsuranceRiskInsuredService.save(insuredDO);

                            List<CgnciReinsuranceRiskLayerVO> riskLayersVO = riskInsured.getRiskLayers();
                            if (VerifyUtils.isNotEmpty(riskLayersVO)) {
                                List<CgnciReinsuranceRiskLayerDO> riskLayersDO = ConvertUtils.convert(riskLayersVO, CgnciReinsuranceRiskLayerDO.class);
                                riskLayersDO.forEach(riskLayer -> {
                                    riskLayer.setLayerId(null);
                                    riskLayer.setReinsuranceRiskId(insuredDO.getRiskInsuredId());
                                    riskLayer.setCreateUser(user.getNameAndCode());
                                    riskLayer.setUpdateUser(user.getNameAndCode());
                                });
                                cgnciReinsuranceRiskLayerService.saveBatch(riskLayersDO);

                            }

                        });
                    }

                });

            }
            // 临分分出信息--再保人
            List<CgnciReinsurerVO> lfDivideOutReinsurers = lfDivideOutVO.getReinsurers();
            if (VerifyUtils.isNotEmpty(lfDivideOutReinsurers)) {
                lfDivideOutReinsurers.forEach(reinsurer -> {
                    CgnciReinsurerDO reinsurerDO = ConvertUtils.convert(reinsurer, CgnciReinsurerDO.class);
                    reinsurerDO.setReinsurerId(null);
                    reinsurerDO.setReinsuranceId(lfDivideOuDO.getReinsuranceId());
                    reinsurerDO.setCreateUser(user.getNameAndCode());
                    reinsurerDO.setUpdateUser(user.getNameAndCode());
                    reinsurerService.save(reinsurerDO);
                    // 临分分出信息--再保人--险类信息
                    List<CgnciReinsurerRiskVO> risks = reinsurer.getRisks();
                    if (VerifyUtils.isNotEmpty(risks)) {
                        risks.forEach(risk -> {
                            CgnciReinsuranceRiskDO riskDO = ConvertUtils.convert(risk, CgnciReinsuranceRiskDO.class);
                            riskDO.setReinsuranceRiskId(null);
                            riskDO.setReinsuranceId(reinsurerDO.getReinsurerId());
                            riskDO.setCreateUser(user.getNameAndCode());
                            riskDO.setUpdateUser(user.getNameAndCode());
                            cgnciReinsuranceRiskService.save(riskDO);

                            // 临分分出信息--再保人--险类信息--标的
                            List<CgnciReinsurerRiskInsuredVO> riskInsureds = risk.getRiskInsureds();
                            if (VerifyUtils.isNotEmpty(riskInsureds)) {
                                riskInsureds.forEach(riskInsured -> {
                                    CgnciReinsuranceRiskInsuredDO insuredDO = ConvertUtils.convert(riskInsured, CgnciReinsuranceRiskInsuredDO.class);
                                    insuredDO.setRiskInsuredId(null);
                                    insuredDO.setReinsuranceRiskId(riskDO.getReinsuranceRiskId());
                                    insuredDO.setCreateUser(user.getNameAndCode());
                                    insuredDO.setUpdateUser(user.getNameAndCode());
                                    cgnciReinsuranceRiskInsuredService.save(insuredDO);

                                    List<CgnciReinsurerRiskLayerVO> riskLayersVO = riskInsured.getRiskLayers();
                                    if (VerifyUtils.isNotEmpty(riskLayersVO)) {
                                        List<CgnciReinsuranceRiskLayerDO> riskLayersDO = ConvertUtils.convert(riskLayersVO, CgnciReinsuranceRiskLayerDO.class);
                                        riskLayersDO.forEach(riskLayer -> {
                                            riskLayer.setLayerId(null);
                                            riskLayer.setReinsuranceRiskId(insuredDO.getRiskInsuredId());
                                            riskLayer.setCreateUser(user.getNameAndCode());
                                            riskLayer.setUpdateUser(user.getNameAndCode());
                                        });
                                        cgnciReinsuranceRiskLayerService.saveBatch(riskLayersDO);

                                    }

                                });
                            }

                        });

                    }
                });

            }
        }


        // 合约分出
        List<CgnciReinsuranceInfoVO> hyDivideOutVOList = insuranceVO.getHyDivideOutVOList();
        if (VerifyUtils.isNotEmpty(hyDivideOutVOList)) {
            hyDivideOutVOList.forEach(hyDivideOutVO -> {
                CgnciReinsuranceInfoDO hyDivideOutDO = ConvertUtils.convert(hyDivideOutVO, CgnciReinsuranceInfoDO.class);
                hyDivideOutDO.setReinsuranceId(null);
                hyDivideOutDO.setBusinessId(businessInsuranceDO.getInsuranceId());
                hyDivideOutDO.setCreateUser(user.getNameAndCode());
                hyDivideOutDO.setUpdateUser(user.getNameAndCode());
                cgnciReinsuranceInfoService.save(hyDivideOutDO);

                List<CgnciReinsuranceRiskVO> risks = hyDivideOutVO.getRisks();
                if (VerifyUtils.isNotEmpty(risks)) {
                    risks.forEach(risk -> {
                        CgnciReinsuranceRiskDO riskDO = ConvertUtils.convert(risk, CgnciReinsuranceRiskDO.class);
                        riskDO.setReinsuranceRiskId(null);
                        riskDO.setReinsuranceId(hyDivideOutDO.getReinsuranceId());
                        riskDO.setCreateUser(user.getNameAndCode());
                        riskDO.setUpdateUser(user.getNameAndCode());
                        cgnciReinsuranceRiskService.save(riskDO);

                        // 合约分出信息--再保人--险类信息--标的
                        List<CgnciReinsuranceRiskInsuredVO> insuredsVO = risk.getRiskInsureds();
                        if (VerifyUtils.isNotEmpty(insuredsVO)) {
                            insuredsVO.forEach(insured -> {
                                CgnciReinsuranceRiskInsuredDO insuredDO = ConvertUtils.convert(insured, CgnciReinsuranceRiskInsuredDO.class);
                                insuredDO.setRiskInsuredId(null);
                                insuredDO.setReinsuranceRiskId(riskDO.getReinsuranceRiskId());
                                insuredDO.setCreateUser(user.getNameAndCode());
                                insuredDO.setUpdateUser(user.getNameAndCode());
                                cgnciReinsuranceRiskInsuredService.save(insuredDO);

                                List<CgnciReinsuranceRiskLayerVO> layers = insured.getRiskLayers();
                                if (VerifyUtils.isNotEmpty(layers)) {
                                    List<CgnciReinsuranceRiskLayerDO> riskLayersDO = ConvertUtils.convert(layers, CgnciReinsuranceRiskLayerDO.class);
                                    riskLayersDO.forEach(riskLayer -> {
                                        riskLayer.setLayerId(null);
                                        riskLayer.setReinsuranceRiskId(insuredDO.getRiskInsuredId());
                                        riskLayer.setCreateUser(user.getNameAndCode());
                                        riskLayer.setUpdateUser(user.getNameAndCode());
                                    });
                                    cgnciReinsuranceRiskLayerService.saveBatch(riskLayersDO);
                                }

                            });
                        }

                    });
                }
            });
        }

        // 理赔条件
        List<CgnciClaimBasisVO> claimBasisVOList = insuranceVO.getClaimBasisVOList();
        if (VerifyUtils.isNotEmpty(claimBasisVOList)) {
            List<CgnciClaimBasisDO> claimBasisDOList = ConvertUtils.convert(claimBasisVOList, CgnciClaimBasisDO.class);
            claimBasisDOList.forEach(claim -> {
                claim.setBasisId(null);
                claim.setBusinessId(businessInsuranceDO.getInsuranceId());
                claim.setCreateUser(user.getNameAndCode());
                claim.setUpdateUser(user.getNameAndCode());
            });
            claimBasisService.saveBatch(claimBasisDOList);

        }

        // 事故限额
        List<CgnciAccidentLimitVO> accidentLimitVOList = insuranceVO.getAccidentLimitVOList();
        if (VerifyUtils.isNotEmpty(accidentLimitVOList)) {
            List<CgnciAccidentLimitDO> accidentLimitDOList = ConvertUtils.convert(accidentLimitVOList, CgnciAccidentLimitDO.class);
            accidentLimitDOList.forEach(limit -> {
                limit.setLimitId(null);
                limit.setBusinessId(businessInsuranceDO.getInsuranceId());
                limit.setCreateUser(user.getNameAndCode());
                limit.setUpdateUser(user.getNameAndCode());
            });
            accidentLimitService.saveBatch(accidentLimitDOList);

        }

        // 特约条件
        CgnciSpecialConditionVO specialConditionVO = insuranceVO.getSpecialConditionVO();
        if (VerifyUtils.isNotEmpty(specialConditionVO)) {
            CgnciSpecialConditionDO specialConditionDO = ConvertUtils.convert(specialConditionVO, CgnciSpecialConditionDO.class);
            specialConditionDO.setConditionId(null);
            specialConditionDO.setBusinessId(businessInsuranceDO.getInsuranceId());
            specialConditionDO.setConditionType("1");
            specialConditionDO.setCreateUser(user.getNameAndCode());
            specialConditionDO.setUpdateUser(user.getNameAndCode());
            specialConditionService.save(specialConditionDO);
        }

        // 除外条件
        CgnciSpecialConditionVO exclusionConditionVO = insuranceVO.getExclusionConditionVO();
        if (VerifyUtils.isNotEmpty(exclusionConditionVO)) {
            CgnciSpecialConditionDO exclusionConditionDO = ConvertUtils.convert(exclusionConditionVO, CgnciSpecialConditionDO.class);
            exclusionConditionDO.setConditionId(null);
            exclusionConditionDO.setBusinessId(businessInsuranceDO.getInsuranceId());
            exclusionConditionDO.setConditionType("2");
            exclusionConditionDO.setCreateUser(user.getNameAndCode());
            exclusionConditionDO.setUpdateUser(user.getNameAndCode());
            specialConditionService.save(exclusionConditionDO);
        }

        // 文件信息
        List<CgnciAccessoryVO> accessoryVOList = insuranceVO.getAccessoryVOList();
        if (VerifyUtils.isNotEmpty(accessoryVOList)) {
            List<CgnciAccessoryDO> accessoryList = new ArrayList<>();
            accessoryVOList.forEach(accessory -> {
                CgnciAccessoryDO accessoryDO = accessoryService.getById(accessory.getAccId());
                // 设置插入数据
                accessoryDO.setAccId(null);
                accessoryDO.setBusinessId(businessInsuranceDO.getInsuranceId());
                accessoryDO.setCreateUser(user.getNameAndCode());
                accessoryDO.setUpdateUser(user.getNameAndCode());
                accessoryList.add(accessoryDO);
            });
            accessoryService.saveBatch(accessoryList);
        }
    }

    @Override
    public void nextDivideOutCreate(CgnciEndorsementInsuranceNextCreateDTO addCreateDTO, Long endorsementId) {
        // 获取当前登录用户
        UserEntity user = UserContext.getUser();
        // 获取原信息
        CgnciBusinessInsuranceMainVO insuranceVO = cgnciBusinessInsuranceService.detail(addCreateDTO.getInsuranceId(), "3");

        // 基本信息
        CgnciBusinessInsuranceDO businessInsuranceDO = ConvertUtils.convert(insuranceVO.getBusinessInsuranceVO(), CgnciBusinessInsuranceDO.class);
        businessInsuranceDO.setInsuranceId(null);
        // 设置该数据为镜像数据
        businessInsuranceDO.setIsMirror("1");
        businessInsuranceDO.setStatus("0");
        businessInsuranceDO.setIsEffective("0");
        businessInsuranceDO.setLastEndorsementId(endorsementId);
        businessInsuranceDO.setCreateUser(user.getNameAndCode());
        businessInsuranceDO.setCreateTime(new Date());
        businessInsuranceDO.setUpdateUser(user.getNameAndCode());
        businessInsuranceDO.setUpdateTime(new Date());
        cgnciBusinessInsuranceService.save(businessInsuranceDO);

        // 合约分出
        List<CgnciReinsuranceInfoVO> hyDivideOutVOList = insuranceVO.getHyDivideOutVOList();
        if (VerifyUtils.isNotEmpty(hyDivideOutVOList)) {
            hyDivideOutVOList.forEach(hyDivideOutVO -> {
                CgnciReinsuranceInfoDO hyDivideOutDO = ConvertUtils.convert(hyDivideOutVO, CgnciReinsuranceInfoDO.class);
                hyDivideOutDO.setReinsuranceId(null);
                hyDivideOutDO.setBusinessId(businessInsuranceDO.getInsuranceId());
                hyDivideOutDO.setCreateUser(user.getNameAndCode());
                hyDivideOutDO.setUpdateUser(user.getNameAndCode());
                cgnciReinsuranceInfoService.save(hyDivideOutDO);

                List<CgnciReinsuranceRiskVO> risks = hyDivideOutVO.getRisks();
                if (VerifyUtils.isNotEmpty(risks)) {
                    risks.forEach(risk -> {
                        CgnciReinsuranceRiskDO riskDO = ConvertUtils.convert(risk, CgnciReinsuranceRiskDO.class);
                        riskDO.setReinsuranceRiskId(null);
                        riskDO.setReinsuranceId(hyDivideOutDO.getReinsuranceId());
                        riskDO.setCreateUser(user.getNameAndCode());
                        riskDO.setUpdateUser(user.getNameAndCode());
                        cgnciReinsuranceRiskService.save(riskDO);

                        // 合约分出信息--再保人--险类信息--标的
                        List<CgnciReinsuranceRiskInsuredVO> insuredsVO = risk.getRiskInsureds();
                        if (VerifyUtils.isNotEmpty(insuredsVO)) {
                            insuredsVO.forEach(insuredsVO1 -> {
                                CgnciReinsuranceRiskInsuredDO riskInsuredDO = ConvertUtils.convert(insuredsVO1, CgnciReinsuranceRiskInsuredDO.class);
                                riskInsuredDO.setRiskInsuredId(null);
                                riskInsuredDO.setReinsuranceRiskId(riskDO.getReinsuranceRiskId());
                                riskInsuredDO.setCreateUser(user.getNameAndCode());
                                riskInsuredDO.setUpdateUser(user.getNameAndCode());
                                cgnciReinsuranceRiskInsuredService.save(riskInsuredDO);

                                List<CgnciReinsuranceRiskLayerVO> layers = insuredsVO1.getRiskLayers();
                                if (VerifyUtils.isNotEmpty(layers)) {
                                    layers.forEach(layer -> {
                                        CgnciReinsuranceRiskLayerDO layersDO = ConvertUtils.convert(layer, CgnciReinsuranceRiskLayerDO.class);
                                        layersDO.setLayerId(null);
                                        layersDO.setReinsuranceRiskId(riskInsuredDO.getRiskInsuredId());
                                        layersDO.setCreateUser(user.getNameAndCode());
                                        layersDO.setUpdateUser(user.getNameAndCode());
                                        cgnciReinsuranceRiskLayerService.save(layersDO);

                                        // 交易对手
                                        List<CgnciCounterpartyVO> counterpartyVOList = layer.getCounterpartyVOList();
                                        if (VerifyUtils.isNotEmpty(counterpartyVOList)) {
                                            List<CgnciReinsurerDO> counterpartyDOList = ConvertUtils.convert(counterpartyVOList, CgnciReinsurerDO.class);
                                            counterpartyDOList.forEach(counterparty -> {
                                                counterparty.setReinsurerId(null);
                                                counterparty.setReinsuranceId(layersDO.getLayerId());
                                                counterparty.setCreateUser(user.getNameAndCode());
                                                counterparty.setUpdateUser(user.getNameAndCode());
                                            });
                                            reinsurerService.saveBatch(counterpartyDOList);
                                        }
                                    });
                                }
                            });
                        }

                    });
                }
            });
        }

        // 费用/账单相关
        CgnciBillInfoVO billInfoVO = insuranceVO.getBillInfoVO();
        if (VerifyUtils.isNotEmpty(billInfoVO)) {
            CgnciBillInfoDO billInfoDO = ConvertUtils.convert(billInfoVO, CgnciBillInfoDO.class);
            billInfoDO.setBillId(null);
            billInfoDO.setBusinessId(businessInsuranceDO.getInsuranceId());
            billInfoDO.setCreateUser(user.getNameAndCode());
            billInfoDO.setUpdateUser(user.getNameAndCode());
            billInfoService.save(billInfoDO);
        }

        // 币种
        List<CgnciExchangeRateVO> exchangeRateVOList = insuranceVO.getExchangeRateVOList();
        if (VerifyUtils.isNotEmpty(exchangeRateVOList)) {
            List<CgnciExchangeRateDO> exchangeRateDOList = ConvertUtils.convert(exchangeRateVOList, CgnciExchangeRateDO.class);
            exchangeRateDOList.forEach(rate -> {
                rate.setRateId(null);
                rate.setBusinessId(businessInsuranceDO.getInsuranceId());
                rate.setCreateUser(user.getNameAndCode());
                rate.setUpdateUser(user.getNameAndCode());
            });
            exchangeRateService.saveBatch(exchangeRateDOList);

        }

        // 理赔条件
        List<CgnciClaimBasisVO> claimBasisVOList = insuranceVO.getClaimBasisVOList();
        if (VerifyUtils.isNotEmpty(claimBasisVOList)) {
            List<CgnciClaimBasisDO> claimBasisDOList = ConvertUtils.convert(claimBasisVOList, CgnciClaimBasisDO.class);
            claimBasisDOList.forEach(claim -> {
                claim.setBasisId(null);
                claim.setBusinessId(businessInsuranceDO.getInsuranceId());
                claim.setCreateUser(user.getNameAndCode());
                claim.setUpdateUser(user.getNameAndCode());
            });
            claimBasisService.saveBatch(claimBasisDOList);

        }

        // 事故限额
        List<CgnciAccidentLimitVO> accidentLimitVOList = insuranceVO.getAccidentLimitVOList();
        if (VerifyUtils.isNotEmpty(accidentLimitVOList)) {
            List<CgnciAccidentLimitDO> accidentLimitDOList = ConvertUtils.convert(accidentLimitVOList, CgnciAccidentLimitDO.class);
            accidentLimitDOList.forEach(limit -> {
                limit.setLimitId(null);
                limit.setBusinessId(businessInsuranceDO.getInsuranceId());
                limit.setCreateUser(user.getNameAndCode());
                limit.setUpdateUser(user.getNameAndCode());
            });
            accidentLimitService.saveBatch(accidentLimitDOList);

        }

        // 特约条件
        CgnciSpecialConditionVO specialConditionVO = insuranceVO.getSpecialConditionVO();
        if (VerifyUtils.isNotEmpty(specialConditionVO)) {
            CgnciSpecialConditionDO specialConditionDO = ConvertUtils.convert(specialConditionVO, CgnciSpecialConditionDO.class);
            specialConditionDO.setConditionId(null);
            specialConditionDO.setBusinessId(businessInsuranceDO.getInsuranceId());
            specialConditionDO.setConditionType("1");
            specialConditionDO.setCreateUser(user.getNameAndCode());
            specialConditionDO.setUpdateUser(user.getNameAndCode());
            specialConditionService.save(specialConditionDO);
        }

        // 除外条件
        CgnciSpecialConditionVO exclusionConditionVO = insuranceVO.getExclusionConditionVO();
        if (VerifyUtils.isNotEmpty(exclusionConditionVO)) {
            CgnciSpecialConditionDO exclusionConditionDO = ConvertUtils.convert(exclusionConditionVO, CgnciSpecialConditionDO.class);
            exclusionConditionDO.setConditionId(null);
            exclusionConditionDO.setBusinessId(businessInsuranceDO.getInsuranceId());
            exclusionConditionDO.setConditionType("2");
            exclusionConditionDO.setCreateUser(user.getNameAndCode());
            exclusionConditionDO.setUpdateUser(user.getNameAndCode());
            specialConditionService.save(exclusionConditionDO);
        }

        // 文件信息
        List<CgnciAccessoryVO> accessoryVOList = insuranceVO.getAccessoryVOList();
        if (VerifyUtils.isNotEmpty(accessoryVOList)) {
            List<CgnciAccessoryDO> accessoryList = new ArrayList<>();
            accessoryVOList.forEach(accessory -> {
                CgnciAccessoryDO accessoryDO = accessoryService.getById(accessory.getAccId());
                // 设置插入数据
                accessoryDO.setAccId(null);
                accessoryDO.setBusinessId(businessInsuranceDO.getInsuranceId());
                accessoryDO.setCreateUser(user.getNameAndCode());
                accessoryDO.setUpdateUser(user.getNameAndCode());
                accessoryList.add(accessoryDO);
            });
            accessoryService.saveBatch(accessoryList);
        }
    }

    @Override
    public Boolean isEndorsement(Long insuranceId) {
        // 查询保单是否存在批改流程
        return this.baseMapper.getIsEndorsement(insuranceId) > 0;
    }

    private String getOperationType(String businessType, String type) {
        String operationType = "";
        switch (type) {
            case "signOut":
                if ("1".equals(businessType)) {
                    operationType = OperationTypeEnum.CANCEL_ENDO_POLICY.getCode();
                } else if ("2".equals(businessType)) {
                    operationType = OperationTypeEnum.CANCEL_REINSURANCE_CHANGE.getCode();
                } else {
                    operationType = OperationTypeEnum.CANCEL_OUTGOING_CONTRACT_CHANGE.getCode();
                }
                break;
            case "submit":
                if ("1".equals(businessType)) {
                    operationType = OperationTypeEnum.SUBMIT_ENDO_POLICY.getCode();
                } else if ("2".equals(businessType)) {
                    operationType = OperationTypeEnum.SUBMIT_REINSURANCE_CHANGE_AUDIT.getCode();
                } else {
                    operationType = OperationTypeEnum.SUBMIT_OUTGOING_CONTRACT_AUDIT.getCode();
                }
                break;
            case "back":
                if ("1".equals(businessType)) {
                    operationType = OperationTypeEnum.ENDO_POLICY_BACK.getCode();
                } else if ("2".equals(businessType)) {
                    operationType = OperationTypeEnum.REINSURANCE_CHANGE_AUDIT_BACK.getCode();
                } else {
                    operationType = OperationTypeEnum.OUTGOING_CONTRACT_CHANGE_AUDIT_BACK.getCode();
                }
                break;
            case "pass":
                if ("1".equals(businessType)) {
                    operationType = OperationTypeEnum.ENDO_POLICY_PASS.getCode();
                } else if ("2".equals(businessType)) {
                    operationType = OperationTypeEnum.REINSURANCE_CHANGE_AUDIT_PASS.getCode();
                } else {
                    operationType = OperationTypeEnum.OUTGOING_CONTRACT_CHANGE_AUDIT_PASS.getCode();
                }
                break;
            default:
                throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR);
        }
        return operationType;
    }

}
